package com.start.platform.wx.config;

import com.start.common.tools.StartRedisTools;
import me.chanjar.weixin.common.util.locks.RedisTemplateSimpleDistributedLock;
import me.chanjar.weixin.open.api.impl.AbstractWxOpenInRedisConfigStorage;

import java.util.concurrent.locks.Lock;

public class WxOpenRedisConfigStorage extends AbstractWxOpenInRedisConfigStorage {

    private StartRedisTools startRedisTools;

    public WxOpenRedisConfigStorage(StartRedisTools startRedisTools) {
        this(startRedisTools, null);
    }


    public WxOpenRedisConfigStorage(StartRedisTools startRedisTools, String keyPrefix) {
        this.startRedisTools = startRedisTools;
        this.keyPrefix = keyPrefix;
    }

    @Override
    public String getComponentVerifyTicket() {
        if (!startRedisTools.hasKey(this.componentVerifyTicketKey)) {
            return null;
        }
        return startRedisTools.get(this.componentVerifyTicketKey).toString();
    }

    @Override
    public void setComponentVerifyTicket(String componentVerifyTicket) {
        startRedisTools.set(this.componentVerifyTicketKey, componentVerifyTicket, Integer.MAX_VALUE);
    }

    @Override
    public String getComponentAccessToken() {
        if (!startRedisTools.hasKey(this.componentAccessTokenKey)) {
            return null;
        }
        return startRedisTools.get(this.componentAccessTokenKey).toString();
    }

    @Override
    public boolean isComponentAccessTokenExpired() {
        long expire = startRedisTools.getExpire(this.componentAccessTokenKey);
        return expire < 2;
    }

    @Override
    public void expireComponentAccessToken() {
        startRedisTools.expire(this.componentAccessTokenKey, 0);
    }

    @Override
    public void updateComponentAccessToken(String componentAccessToken, int expiresInSeconds) {
        startRedisTools.set(this.componentAccessTokenKey, componentAccessToken, expiresInSeconds - 200);
    }

    @Override
    public String getAuthorizerRefreshToken(String appId) {
        String key = this.getKey(this.authorizerRefreshTokenKey, appId);
        return startRedisTools.get(key).toString();
    }

    @Override
    public void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) {
        startRedisTools.set(this.getKey(this.authorizerRefreshTokenKey, appId), authorizerRefreshToken, 0);
    }

    @Override
    public String getAuthorizerAccessToken(String appId) {
        return startRedisTools.get(this.getKey(this.authorizerAccessTokenKey, appId)).toString();
    }

    @Override
    public boolean isAuthorizerAccessTokenExpired(String appId) {
        long expire = startRedisTools.getExpire(this.getKey(this.authorizerAccessTokenKey, appId));
        return expire < 2;
    }

    @Override
    public void expireAuthorizerAccessToken(String appId) {
        startRedisTools.expire(this.getKey(this.authorizerAccessTokenKey, appId), 0);
    }

    @Override
    public void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds) {
        startRedisTools.set(this.getKey(this.authorizerAccessTokenKey, appId), authorizerAccessToken, expiresInSeconds - 200);
    }

    @Override
    public String getJsapiTicket(String appId) {
        return startRedisTools.get(this.getKey(this.jsapiTicketKey, appId)).toString();
    }

    @Override
    public boolean isJsapiTicketExpired(String appId) {
        long expire = startRedisTools.getExpire(this.getKey(this.jsapiTicketKey, appId));
        return expire < 2;
    }

    @Override
    public void expireJsapiTicket(String appId) {
        startRedisTools.expire(this.getKey(this.jsapiTicketKey, appId), 0);
    }

    @Override
    public void updateJsapiTicket(String appId, String jsapiTicket, int expiresInSeconds) {
        startRedisTools.set(this.getKey(this.jsapiTicketKey, appId), jsapiTicket, expiresInSeconds - 200);
    }

    @Override
    public String getCardApiTicket(String appId) {
        return startRedisTools.get(this.getKey(this.cardApiTicket, appId)).toString();
    }

    @Override
    public boolean isCardApiTicketExpired(String appId) {
        long expire = startRedisTools.getExpire(this.getKey(this.cardApiTicket, appId));
        return expire < 2;
    }

    @Override
    public void expireCardApiTicket(String appId) {
        startRedisTools.expire(this.getKey(this.cardApiTicket, appId), 0);
    }

    @Override
    public void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds) {
        startRedisTools.set(this.getKey(this.cardApiTicket, appId), cardApiTicket, expiresInSeconds - 200);
    }

    @Override
    public Lock getLockByKey(String key) {
        return new RedisTemplateSimpleDistributedLock(startRedisTools.getStringRedisTemplate(), key, 60 * 1000);
    }
}
